#include "musicslider.h"
#include "ui_musicslider.h"
#include <QMouseEvent>
#include <QDebug>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider),
    currentPos(0)
{
    ui->setupUi(this);
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setStep(float ratio)
{
    currentPos = ui->outLine->maximumWidth() * ratio;
    moveSilder();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSilder();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    (void)event;
    if (event->buttons() == Qt::LeftButton)
    {
        currentPos = event->pos().x();
        if (currentPos < 0)
        {
            currentPos = 0;
        }

        if (currentPos > ui->outLine->maximumWidth())
        {
            currentPos = ui->outLine->maximumWidth();
        }

        moveSilder();
    }
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    moveSilder();
    emit setMusicSliderPosition(currentPos / (float)ui->outLine->maximumWidth());
}

void MusicSlider::moveSilder()
{
    ui->outLine->setGeometry(ui->outLine->x(), ui->outLine->y(), currentPos, ui->outLine->height());
}
